package cn.xi.controller;

import cn.xi.checkItem.SetmealService;
import cn.xi.constant.MessageConstant;
import cn.xi.entity.PageResult;
import cn.xi.entity.QueryPageBean;
import cn.xi.entity.Result;
import cn.xi.pojo.Setmeal;
import cn.xi.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/findpage")
    public PageResult pages(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.pages(queryPageBean);
    }

    @RequestMapping("/findall")
    public Result findall() {
        return setmealService.findall();
    }

    @RequestMapping("/save")
    public Result save(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        jedisPool.getResource().sadd("shujuku", setmeal.getImg());
        Result save = setmealService.save(setmeal, checkgroupIds);
        if (save.getData() != null) {
            String data = (String) save.getData();
            jedisPool.getResource().srem("shujuku", data);
        }
        return save;
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //得到传过来的文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取后缀名
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i - 1);
        //生成uuid并拼接字符串,生成新的文件名
        String s = UUID.randomUUID().toString() + substring;
        try {
            //上传文件
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), s);
            jedisPool.getResource().sadd("qiniu", s);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, s);
    }


    @RequestMapping("/selectsetmeal")
    public Map selectsetmeal(Integer id) {
        return setmealService.selectsetmeal(id);
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody Setmeal setmeal) {
        try {
            jedisPool.getResource().srem("shujuku", setmeal.getImg());
            setmealService.delete(setmeal.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "删除套餐失败");
        }
        return new Result(true, "删除套餐成功");
    }

}
